import { getVerificationRequest } from '@/api/login'
import { getVerificationCodeRequest } from '@/api/user'

export default {
  data() {
    let validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.sizeForm.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    }
    // let validatePass2 = (rule, value, callback) => {
    //     if (value === "") {
    //       callback(new Error("请输入您的密码"));
    //     } else if (value === this.sizeForm.username) {
    //       callback(new Error("密码不能与用户名相同!"));
    //     } else {
    //       callback();
    //     }
    //   }
    return {
      forgotRules: {
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[189]))\d{8}$/, message: '请输入正确的手机号码', trigger: ['change', 'blur'] }
        ],
        Verification: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
          { pattern: /^[0-9]{6}$/, message: '请输入6位验证码', trigger: ['change', 'blur'] }
        ],
        // username: [
        //   { required: true, message: '请设置您的用户名', trigger: 'blur' },
        //   { pattern:/^(?!^\d+$)(?!^\W+$)[a-zA-Z0-9_]{6,32}$/, message: '用户名不符合规范', trigger: ['change','blur'] },
        // ], 
        password: [
          { required: true, message: '请输入您的密码', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9_]{6,20}$/, message: '密码长度为6-20位', trigger: ['change', 'blur'] },
          // { required: true, validator: validatePass2, trigger: ['change','blur'] },

        ],
        passwordAgain: [
          { required: true, message: '请输入再次您的密码', trigger: 'blur' },
          { required: true, validator: validatePass, trigger: ['change', 'blur'] },
          // { required: true, validator: validatePass2, trigger: ['change','blur'] },
        ],
        ischecked: [
          { pattern: /^(true)$/, message: '请勾选同意协议', trigger: 'change' }
        ]
      },
      changeRiles: {
        validCode: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
          { pattern: /^[0-9]{6}$/, message: '请输入6位验证码', trigger: ['change', 'blur'] }
        ],
        password: [
          { required: true, message: '请输入您的密码', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9_]{6,20}$/, message: '密码长度为6-20位', trigger: ['change', 'blur'] },

        ],
        passwordAgain: [
          { required: true, message: '请输入再次您的密码', trigger: 'blur' },
          { required: true, validator: validatePass, trigger: ['change', 'blur'] },
        ],
      }
    }
  },
  methods: {
    //验证码校验
    validationText() {
      if (!this.timer) {
        this.$refs['forgotForm'].validateField(['phone'], async err => {
          if (!err) {
            try {
              const res = await getVerificationRequest({ mobilePhone: this.sizeForm.phone })
              if (res.code !== 200) return this.$message({
                message: res.message,
                type: 'warning'
              })
              this.validation = `60s后重新发送`;
              this.timer = setInterval(() => {
                if (this.countdown > 0 && this.countdown <= 60) {
                  this.countdown--;
                  if (this.countdown !== 0) {
                    this.validation = `${this.countdown}s后重新发送`;
                  } else {
                    clearInterval(this.timer);
                    this.validation = "重新发送";
                    this.countdown = 60;
                    this.timer = null;
                    this.codeDisabled = false;
                  }
                }
              }, 1000)
            } catch (error) {
              this.$message.error('系统错误，请联系管理员！')
            }
          }
        })
      }
    },
    //验证码校验
    async sendValidation() {
      if (!this.timer) {
        try {
          const res = await getVerificationCodeRequest({
            mobilePhone: this.bindPhone,
            validCodeTypeName: 'phone_modify_password'
          })
          if (res.code !== 200) return this.$message({
            message: res.message,
            type: 'warning'
          });
          this.validation = `60s后重新发送`;
          this.timer = setInterval(() => {
            if (this.countdown > 0 && this.countdown <= 60) {
              this.countdown--;
              if (this.countdown !== 0) {
                this.validation = `${this.countdown}s后重新发送`;
              } else {
                clearInterval(this.timer);
                this.validation = "重新发送";
                this.countdown = 60;
                this.timer = null;
                this.codeDisabled = false;
              }
            }
          }, 1000)
        } catch (error) {
          this.$message.error('系统错误，请联系管理员！')
        }
      }
    },
  }
}